﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;

/// <summary>
/// Summary description for ManipulationBLL
/// </summary>
public class UltisCode
{
    /// <summary>
    /// Tạo chuỗi ramdom
    /// </summary>
    /// <param name="strLength"></param>
    /// <returns></returns>
    public static string CreateStringRandom(int strLength)
    {
        const string allowedChars = "abcdefghijkmnopqrstuvwxyz0123456789_-";
        Random rd = new Random();

        char[] chars = new char[strLength];
        int allowedCharsCount = allowedChars.Length;

        for (int i = 0; i < strLength; i++)
        {
            chars[i] = allowedChars[(int)(allowedCharsCount * rd.NextDouble())];
        }
        return new string(chars);
    }
    public static string CreateNewImageName(string name, int length)
    {
        string extent = name.Substring(name.LastIndexOf('.'), (name.Length - name.LastIndexOf('.')));
        return CreateStringRandom(length) + extent;
    }

    /// <summary>
    /// Đưa vào chuỗi text, đưa ra chuỗi mã hóa
    /// </summary>
    /// <param name="strInput"></param>
    /// <returns></returns>
    public static string ChangeMD5(string strInput)
    {
        using (MD5CryptoServiceProvider crypMD5 = new MD5CryptoServiceProvider())
        {
            byte[] bt = Encoding.UTF8.GetBytes(strInput);
            bt = crypMD5.ComputeHash(bt);
            StringBuilder str = new StringBuilder();
            foreach (byte item in bt)
            {
                str.Append(item.ToString("x2").ToLower());
            }
            return str.ToString();
        }
    }


    /// <summary>
    /// Kiểm tra chuỗi có phải là số k?
    /// </summary>
    /// <param name="strInput"></param>
    /// <returns></returns>
    public static bool CheckInt(string strInput)
    {
        return new Regex("^(\\d+)$").IsMatch(strInput);
    }



    /// <summary>
    /// Kiểm tra chuỗi có phải số đt
    /// </summary>
    /// <returns></returns>
    public static bool CheckNumberPhone(string strInput)
    {
        Regex regex = new Regex(@"^[\\(]{0,1}([0-9]){3}[\\)]{0,1}[ ]?([^0-1]){1}([0-9]){2}[ ]?[-]?[ ]?([0-9]){4}[ ]*((x){0,1}([0-9]){1,5}){0,1}$");

        bool result = regex.IsMatch(strInput);
        if (result)
            return true;
        return false;
    }


    /// <summary>
    /// Kiểm tra chuỗi có phải số đt
    /// </summary>
    /// <returns></returns>
    public static bool CheckEmail(string strInput)
    {
        Regex regex = new Regex(@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$");

        bool result = regex.IsMatch(strInput);
        if (result)
            return true;
        return false;
    }


    public static void Notification(Page page, string message)
    {
        string prompt = "<script>$('#one').jGrowl('{0}');</script>";
        string messageShow = String.Format(prompt, message);
        ScriptManager.RegisterStartupScript(page, typeof(Page), "alert", messageShow, false);
    }
}